<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
session_start();
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	public $uses=array('Category','Download');
	public $components = array(
		'Session',
		'Security',
		'Auth' => array(
            'loginRedirect' => array('controller' => 'jobs', 'action' => 'promoted'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
            'authorize' => array('Controller'),
            'unauthorizedRedirect' => false
           
        ),
		'Search.Prg',
         'Croogo',
         'RequestHandler',
         'Recaptcha.Recaptcha',
        
	);
	public $helper = array(
		'layout',
		'Form',
		'Html',
		'Paginator',
		'Job',
		'Countries',
		'Custom',
		
		'Text',
		
		'Tree',
		'Documents.Listcategory',
		'Recaptcha.Recaptcha'
		
		
		//'Security',
	);
	public function isAuthorized($user = null) {
        // Any registered user can access public functions
        if (empty($this->request->params['admin'])) {
        	
            return true;
        }

        // Only admins can access admin functions
        if (isset($this->request->params['admin'])) {
            return (bool)($user['role_id'] == 1);
        }

        // Default deny
        return false;
    }

	 


	public function beforeFilter(){

      //  $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false);
	//	$this->Auth->loginRedirect = array('controller' => 'orders', 'action' => 'index', 'admin' => true);
	//	$this->Auth->logoutRedirect = array('controller' => 'products', 'action' => 'index', 'admin' => false);
	//$this->Auth->authorize = array('Controller');

		$this->Auth->authenticate = array(
			AuthComponent::ALL => array(
				'userModel' => 'User',
				'fields' => array(
					'username' => 'username',
					'password' => 'password'
				),
				'scope' => array(
					'User.status' => 1,
				)
			), 'Form'
		);

		//$this->Auth->loginError =" Bạn không có quyền truy cập" ;
        $this->Auth->allow('view','users/add','tutors/add','customer_add','customer_view','index','promoted','getlist1','getlist','verify','view_home','customer_index');
		
		if (isset($this->request->params['admin'])) {
			$this->layout='admin';
                        
                }

         
	   
	   $this->Category->unbindModel(array('hasMany'=>array('Download')));
		$list = $this->Category->find('threaded',array('order'=>array('Category.lft ASC'),'conditions' => array( 'Category.parent_id !='=>null
			),'fields'=>array('Category.id','Category.name','Category.parent_id','Category.slug')));
		$this->set('list_cat',$list);
		
 

    // For default settings name must be menu

		
        
	
	    }
	public function admin_switch($field = null, $id = null) {
        $this->autoRender = false;
        $model = $this->modelClass;
        if ($this->$model && $field && $id) {
            $field = $this->$model->escapeField($field);
            return $this->$model->updateAll(array($field => '1 -' . $field), array($this->$model->escapeField() => $id));
        }
        if(!$this->RequestHandler->isAjax()) {
            return $this->redirect($this->referer());
        }
    }           

}
